
import { _decorator, v3, Node, tween } from 'cc';
import BasePanel from '../../core/component/BasePanel';
import WindowManager from '../../core/manager/WindowManager';
import Data from '../../core/manager/Data';
import Utils from '../../core/utils/Utils';
import { getCurLevelInfo } from '../../../configs/Levels';
import { ITEM_FROZEN_LV, ITEM_HINT_LV, ITEM_TYPE } from '../../core/common/GameConst';
import { BuyItemLayer } from './gameui/BuyItemLayer';
import MsgHints from '../../core/utils/MsgHints';
import GameConst from '../../core/common/GameConst';
import AudioMgr from '../../core/utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('GameUI')
export class GameUI extends BasePanel {
    private hint_time = 0;
    public starNode: Node = null;
    onBtnClicked(event: any, customEventData: any) {
       
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_pause":
                AudioMgr.Instance().playSFX('btnclick')
                this.pasue = true;
                WindowManager.ins.open("PauseLayer")
                break;
            case "item_left":
                AudioMgr.Instance().playSFX('btnclick')
                if (Date.now() - this.hint_time < 2000) return;
                if (Data.user.lv < ITEM_HINT_LV) {
                    MsgHints.show("关卡" + ITEM_HINT_LV + "解锁");
                    return;
                }
                if (Data.user.hint > 0) {
                    this.dispatch(GameConst.USE_ITEM_HINT);
                    this.hint_time = Date.now();
                }
                else {
                    WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
                        com.setItemType(ITEM_TYPE.Hint);
                    })
                }
                break;
            case "item_right":
                if (this.freezetime) {
                    MsgHints.show("冰冻中...")
                    return;
                }
                if (Data.user.lv < ITEM_FROZEN_LV) {
                    MsgHints.show("关卡" + ITEM_FROZEN_LV + "解锁");
                    return;
                }
                if (Data.user.frozen > 0) {
                    Data.user.frozen--;
                    this.freezetime = true;
                    setTimeout(() => {
                        this.freezetime = false;
                    }, 5000);
                    AudioMgr.Instance().playSFX('freeze')
                }
                else {
                    WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
                        com.setItemType(ITEM_TYPE.Frozen);
                    })
                }
                break;
        }
    }

    start() {
        this.register(GameConst.USE_ITEM_TIME, this.doUseTime.bind(this))
        this.register("PAUSE_LAYER_CLOSE", () => {
            this.pasue = false;
        })


        this.starNode = this.GetGameObject("star-with-outline");
    }

    restart() {
        this.SetText("lbl_lv", "关卡" + Data.user.lv);
        let obj = getCurLevelInfo();
        console.log("关卡数据:", obj);
        this.levelTime = obj.levelTime / 2;
        this.GetGameObject("match-timer-freeze-background").active = false;
        this.GetGameObject("match-extra-time-power-up-small").active = false;

        this.SetText("lbl_left_item_lv", "关卡" + ITEM_HINT_LV);
        this.SetText("lbl_right_item_lv", "关卡" + ITEM_FROZEN_LV);
        this.freezetime = false;
        this.pasue = false;
        this.passedTime = 0;
    }

    doUseTime() {
        Data.user.time--;
        let node = this.GetGameObject("match-extra-time-power-up-small");
        node.active = true;
        node.setPosition(v3(0, 0, 0));
        tween(node).to(0.5, { worldPosition: this.GetGameObject("lbl_time").worldPosition }).call(() => {
            this.levelTime += 60;
            node.active = false;
        }).start();
    }

    levelTime = 0;
    passedTime = 0;
    pasue: boolean = false;
    freezetime: boolean = false;

    public star = 0;
    update(dt) {
        
        if(WindowManager.ins.isShow("OutOfBoxLayer")||WindowManager.ins.isShow("DrawStarLayer")||WindowManager.ins.isShow("LevelClearLayer")||WindowManager.ins.isShow("FailLayer"))return
        
        
        this.GetGameObject("match-timer-freeze-background").active = this.freezetime;
        if (!this.pasue && !this.freezetime)
            this.passedTime += dt;

        

        this.SetText("lbl_star", this.star + "");
        let nLeft = this.levelTime - this.passedTime;
        if (nLeft > 0) {
            this.SetText("lbl_time", Utils.getTimeStrByS(nLeft));
        }
        else {
            // console.log("游戏结束")
            WindowManager.ins.open("FailLayer")
        }

        if (Data.user.lv < ITEM_HINT_LV) {
            this.GetGameObject("item_left_Locked").active = true;
            this.GetGameObject("item_left_icon").active = false;
        }
        else {
            this.GetGameObject("item_left_Locked").active = false;
            this.GetGameObject("item_left_icon").active = true;
            this.SetText("lbl_item_left_count", Data.user.hint == 0 ? "+" : Data.user.hint + "");
        }

        if (Data.user.lv < ITEM_FROZEN_LV) {
            this.GetGameObject("item_right_Locked").active = true;
            this.GetGameObject("item_right_icon").active = false;
        }
        else {
            this.GetGameObject("item_right_Locked").active = false;
            this.GetGameObject("item_right_icon").active = true;
            this.SetText("lbl_item_rihgt_count", Data.user.frozen == 0 ? "+" : Data.user.frozen + "");
        }
    }
}
